package JdbcUtils;

import javax.sql.DataSource;
import java.sql.*;

public class JdbcUtils {
    public static DataSource getDataSoure;

    static {
        //1加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //获取连接的方法
    public static Connection connection() throws SQLException {
        //获取连接信息
        Connection connection = DriverManager.getConnection("jdbc:mysql:///test2", "root", "root");
        return connection;

    }

    //释放资源
    public static void close(ResultSet set, Statement statement, Connection connection) {
        //释放Resultset资源
        if (set != null) {
            try {
                set.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
